package com.miya.demo.support.rabbitmq.logrecord;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 日志记录侦听器
 *
 * @author CaiXiaowei
 * @date 2022/10/14
 */
@Component
@Slf4j
public class LogRecordListener {

    @RabbitListener(queues = LogRecordConfig.QUEUE)
    public void onMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String messageStr = new String(message.getBody(), StandardCharsets.UTF_8);
            log.info("LogRecordListener：【{}】", messageStr);
            // TODO 处理消息逻辑

            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("LogRecordListener--消费异常：【{}】", e.getMessage(), e);
            channel.basicAck(deliveryTag, false);
        }
    }
}
